package  com.sxhalo.fsms.export;

import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ExportSetInfo {

	private String titles;
	private String userName;
	private String dateSection;
	private List<String[]> unifiedNum;

	private List<String[]> headNames;

	private String[] fieldNames;

	private List<Map<String, Object>> datalist;

	private OutputStream out;

	public List<Map<String, Object>> getDatalist() {
		return datalist;
	}

	public void setDatalist(List<Map<String, Object>> datalist) {
		this.datalist = datalist;
	}

	public String getTitles() {
		return titles;
	}

	public void setTitles(String titles) {
		this.titles = titles;
	}

	public List<String[]> getUnifiedNum() {
		return unifiedNum;
	}

	public void setUnifiedNum(List<String[]> unifiedNum) {
		this.unifiedNum = unifiedNum;
	}

	public String getDateSection() {
		return dateSection;
	}

	public void setDateSection(String dateSection) {
		this.dateSection = dateSection;
	}

	public int getUnifiedCount() {
		int count = 0;
		if (this.unifiedNum != null) {
			count = this.unifiedNum.size();
		}
		return count;
	}

	public List<String[]> getHeadNames() {
		return headNames;
	}

	public void setHeadNames(List<String[]> headNames) {
		this.headNames = headNames;
	}

	public int getHeadCount() {
		int count = 0;
		if (this.headNames != null) {
			count = this.headNames.size();
		}
		return count;
	}

	public String[] getFieldNames() {
		return fieldNames;
	}

	public void setFieldNames(String[] fieldNames) {
		this.fieldNames = fieldNames;
	}

	public int getFieldCount() {
		int count = 0;
		if (this.fieldNames != null) {
			count = this.fieldNames.length;
		}
		return count;
	}

	public OutputStream getOut() {
		return out;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	/**
	 * @param out
	 *            Excel数据将输出到该输出流
	 */
	public void setOut(OutputStream out) {
		this.out = out;
	}

	/**
	 * @Description: 获取标题
	 */
	public List<HeadRange> getHeadRange() {
		List<HeadRange> headlist = new ArrayList<HeadRange>();
		for (int i = 0; i < headNames.size(); i++) {
			String[] row = headNames.get(i);
			for (int j = 0; j < row.length; j++) {
				HeadRange head = new HeadRange();
				if (isFirst(row[j], i, j)) {
					head.setValue(row[j]);
					head.setFirstRow(i);
					head.setFirstCol(j);
					headlist.add(head);
					setLast(head);
				}
			}
		}
		return headlist;
	}

	private Boolean isFirst(String val, int i, int j) {
		Boolean first = true;
		if (i > 0) {
			if (val == headNames.get(i - 1)[j]) {
				first = false;
			}
		}
		if (j > 0) {
			if (val == headNames.get(i)[j - 1]) {
				first = false;
			}
		}
		return first;
	}

	private	void setLast(HeadRange head) {
		for (int i = head.firstRow; i < headNames.size(); i++) {
			String[] row = headNames.get(i);
			for (int j = head.firstCol; j < row.length; j++) {
				if (head.getValue() == row[j]) {
					head.setLastRow(i);
					head.setLastCol(j);
				} else {
					break;
				}
			}
		}
	}

	public class HeadRange {

		private String cellValue;
		private int firstRow;
		private int lastRow;
		private int firstCol;
		private int lastCol;

		public String getValue() {
			return cellValue;
		}

		public void setValue(String value) {
			this.cellValue = value;
		}

		public int getFirstRow() {
			return firstRow;
		}

		public void setFirstRow(int firstRow) {
			this.firstRow = firstRow;
		}

		public int getLastRow() {
			return lastRow;
		}

		public void setLastRow(int lastRow) {
			this.lastRow = lastRow;
		}

		public int getFirstCol() {
			return firstCol;
		}

		public void setFirstCol(int firstCol) {
			this.firstCol = firstCol;
		}

		public int getLastCol() {
			return lastCol;
		}

		public void setLastCol(int lastCol) {
			this.lastCol = lastCol;
		}

		public String getCellValue() {
			return cellValue;
		}

		public void setCellValue(String cellValue) {
			this.cellValue = cellValue;
		}
	}
}
